//
//  BJGuidePageViewController.m
//  JuJinHuiRong
//
//  Created by APPLE on 16/11/3.
//  Copyright © 2016年 APPLE. All rights reserved.
//

#import "BJGuidePageViewController.h"
#import "BJTabBarViewController.h"

@interface BJGuidePageViewController ()<UIScrollViewDelegate>

@property (nonatomic,retain)UIScrollView * scrollView;
@property (nonatomic,retain)UIPageControl * pageControl;
@property (nonatomic, retain) NSArray * arr;

@end

@implementation BJGuidePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _arr = [[NSArray alloc]init];
    [self createScrollView];
    [self createPageControl];
}

- (void)createScrollView{
    
    _arr = @[@"yin_1",@"yin_2",@"yin_3"];
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    
    //如果与UIPageControl联用 可以设置 启动翻页模式
    _scrollView.pagingEnabled = YES;
    _scrollView.contentSize = CGSizeMake(ScreenWidth * _arr.count, ScreenHeight);
    _scrollView.delegate = self;
    UIImageView * imageView;
    for (int i = 0; i < 3; i++){
       imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:_arr[i]]];
        imageView.frame = CGRectMake(ScreenWidth * i, 0, ScreenWidth, ScreenHeight);
        imageView.userInteractionEnabled = YES;
        [_scrollView addSubview:imageView];
    }
    [self.view addSubview:_scrollView];
    
    UIButton * btn = [Utils createBtnWithType:(UIButtonTypeCustom) backgroundColor:BaseColor title:@"点击进入" image:nil font:15];
      [imageView addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(imageView.mas_bottom).offset(-BILIHEIGHT(100));
        make.centerX.mas_equalTo(imageView.mas_centerX);
        make.left.mas_equalTo(imageView.mas_left).offset((ScreenWidth / 2 - BILIWIDTH(50)));
        make.size.mas_equalTo(CGSizeMake(BILIWIDTH(100), BILIWIDTH(40)));
    }];
    
    BJViewBorderRadius(btn, 10, 1.0, [UIColor redColor]);
    [btn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)Click:(UIButton *)sender{
    //    储存guide状态
    NSUserDefaults * userDef = USERDEFAULTS;
    [userDef setObject:@"1" forKey:@"guide"];
    DLog(@"点击引导页进入程序");
    BJTabBarViewController * main = [[BJTabBarViewController alloc]init];
    self.view.window.rootViewController = main;
}

- (void)createPageControl{
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ScreenHeight - BILIHEIGHT(80), ScreenWidth, BILIHEIGHT(40))];
        _pageControl.numberOfPages = _arr.count;
        _pageControl.backgroundColor = [UIColor clearColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        [_pageControl addTarget:self action:@selector(ClickPage:) forControlEvents:UIControlEventValueChanged];
        _pageControl.currentPage = 0;
    [self.view addSubview:_pageControl];
}

//点击小圆点让scrollerView发生偏移
- (void)ClickPage:(UIPageControl *)page{
    CGPoint point = CGPointMake(_pageControl.currentPage * ScreenWidth, 0);
    [_scrollView setContentOffset:point animated:YES];
}

//移动scrollerView让小圆点偏移
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _pageControl.currentPage = (NSInteger) (scrollView.contentOffset.x /ScreenWidth);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
